/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
*/


package plar.core;



import org.jbox2d.common.Vec2;

/**
 * the Gun is something that initialize a Bullet class
 *
 * @author Antonio Ragagnin
 *
 */
public abstract class Gun {

    public int ammo;
    public ElementPlayer owner;
    public int gunUseAmmo;
    public String gunName;
    public String gunDescription;
    public Gun() {
        super();
        ammo = 0;
    }


    public void fire()
    {
        if (ammo >= gunUseAmmo ) {
            createBullet();
            addAmmo(-gunUseAmmo);
        }


    }


    public abstract void createBullet() ;


    public void initializeShoot(ElementBullet shoot) {


        shoot.owner=owner;
        Vec2 shootPos = getShootRelativePosition();
        Vec2 shootSpeed = getShootRelativeSpeed();

        shootPos.x+= owner.body.getWorldCenter().x;
        shootPos.y+= owner.body.getWorldCenter().y;
        Common.info(7,"shooting direction "+D());
        shootSpeed.x+=D().x*shoot.speed;
        shootSpeed.y+=D().y*shoot.speed;
        shoot.setPosition(shootPos.x,shootPos.y);
        //shoot.applySpeed(shootSpeed); //Ok if shoot is kyn
        owner.level.addElement(shoot);
        shoot.applySpeed(shootSpeed); //Ok if shoot is dyn

    }
    public void addAmmo(int i) {
        ammo += i;
    }



    public Vec2 D()
    {
        if(owner.directionX==0 &&  owner.directionY==0)
            return new Vec2(1,0);
        else {
            Vec2 d=  new Vec2(owner.directionX,owner.directionY);
            return d;
        }

    }

    public Vec2 getShootRelativePosition() {
        return new Vec2(
                   D().x * 2 * owner.getSize().x ,
                   D().y * 2 * owner.getSize().y
               );

    }
    public Vec2 getShootRelativeSpeed() {
        return new Vec2(
                   0,0
               );

    }


}
